package com.es.client.elasticesearch;

import java.net.InetSocketAddress;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.SuggestionSearchContext;
import org.elasticsearch.search.suggest.Suggest.Suggestion;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder;
import org.elasticsearch.search.suggest.term.TermSuggestion;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
import org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option;
import org.elasticsearch.transport.client.PreBuiltTransportClient;


public class ESClient {
	private static String CLUSTER_NAME = "elasticsearch";
	private static String HOST_IP = "119.23.250.150";
	private static int PORT = 9300;
	private Client client;
	private ESClient(){
		this.client = createTransportClient();
	}
	private static class Holder{
		private static final ESClient INSTANCE = new ESClient();
	}
	
	public static ESClient getEsClient(){
		return Holder.INSTANCE;
	}
	
	
	
	/**
	 * 创建TransportClient
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "resource" })
	private Client createTransportClient() {
		Settings settings = Settings.builder().put("cluster.name",CLUSTER_NAME).build();
		InetSocketAddress address = new InetSocketAddress(HOST_IP, PORT);
		TransportAddress transportAddress = new TransportAddress(address);
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(transportAddress);
		return client;
	}
	
	public SearchHits querySearch(String index, String type, SearchSourceBuilder source){
		
		
		SearchRequestBuilder searchRequestBuilder =client.prepareSearch(index)
		        .setTypes(type)
		        // 设置查询类型
		        // 1.SearchType.DFS_QUERY_THEN_FETCH = 精确查询s
				// 2.SearchType.SCAN = 扫描查询,无序
				// 3.SearchType.COUNT = 不设置的话,这个为默认值,还有的自己去试试吧
		        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				// 设置是否按查询匹配度排序
				.setExplain(true)
				// 最后就是返回搜索响应信息
				.setSource(source);
		SearchResponse response = searchRequestBuilder
        .execute()
        .actionGet();
		
		SearchHits searchHits = response.getHits();
		
		return searchHits;
	}
	
	
	public SearchHits suggestion(String index, String type, SuggestBuilder suggest) {
		
		SearchRequestBuilder searchRequestBuilder =client.prepareSearch(index)
		        .setTypes(type)
		        // 设置查询类型
		        // 1.SearchType.DFS_QUERY_THEN_FETCH = 精确查询s
				// 2.SearchType.SCAN = 扫描查询,无序
				// 3.SearchType.COUNT = 不设置的话,这个为默认值,还有的自己去试试吧
		        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				// 设置是否按查询匹配度排序
				.setExplain(true);
		searchRequestBuilder.suggest(suggest);
		System.out.println(searchRequestBuilder.toString());
		SearchResponse response = searchRequestBuilder
				
		        .execute()
		        .actionGet();
				SearchHits searchHits = response.getHits();
				
				return searchHits;
	}

}
